<?php


namespace addon\hotel\model;

use app\model\BaseModel;
use app\model\hotel\Stat;

class HotelCollect extends BaseModel
{
    /**
     * 添加收藏
     * @param array $data
     */
    public function addCollect($data)
    {
        $res = model('hotel_collect')->getInfo([ [ 'member_id', '=', $data['member_id'] ], [ 'hotel_id', '=', $data['hotel_id'] ] ], 'hotel_id, collect_id');
        if (empty($res)) {
            $data['create_time'] = time();
            $collect_id = model('hotel_collect')->add($data);
            //添加统计
            $stat = new Stat();

            $stat->addHotelStat([ 'collect_count' => 1, 'hotel_id' => $data['hotel_id'] ]);
            return $this->success($collect_id);
        } else {
            return $this->error();
        }

    }

    /**
     * 取消收藏
     * @param int $member_id
     * @param int $goods_id
     */
    public function deleteCollect($member_id, $hotel_id)
    {
        $collect_info = model('hotel_collect')->getInfo([ [ 'member_id', '=', $member_id ], [ 'hotel_id', '=', $hotel_id ] ], 'site_id, collect_id');
        if(!empty($collect_info)){
            $res = model('hotel_collect')->delete([ [ 'member_id', '=', $member_id ], [ 'hotel_id', '=', $hotel_id ] ]);
            if ($res) {
                //添加统计
                $stat = new Stat();
                $stat->addHotelStat([ 'collect_count' => -1, 'hotel_id' => $collect_info['hotel_id'] ]);
            }
            return $this->success($res);
        }else{
            return $this->error();
        }

    }

    public function bathDeleteCollect($member_id, $hotel_id)
    {
        $hotel_id_arr = explode(',',$hotel_id);
        foreach ($hotel_id_arr as $k=>$v){
            $collect_info = model('hotel_collect')->getInfo([ [ 'member_id', '=', $member_id ], [ 'hotel_id', '=', $v ] ], 'hotel_id, collect_id');
            if(!empty($collect_info)){
                $res = model('hotel_collect')->delete([ [ 'member_id', '=', $member_id ], [ 'hotel_id', '=', $v ] ]);
                if ($res) {
                    //添加统计
                    $stat = new Stat();
                    $stat->addHotelStat([ 'collect_count' => -1, 'hotel_id' => $collect_info['hotel_id'] ]);
                }
            }
        }
        return $this->success();
    }


    /**
     * 检测商品是否收藏
     * @param $goods_id
     * @param $member_id
     * @return array
     */
    public function getIsCollect($hotel_id, $member_id)
    {
        $res = model('hotel_collect')->getInfo([ [ 'member_id', '=', $member_id ], [ 'hotel_id', '=', $hotel_id ] ], 'collect_id');
        if (!empty($res)) {
            return $this->success(1);
        } else {
            return $this->success(0);
        }
    }

    /**
     * 获取收藏列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getCollectList($condition = [], $field = '*', $order = '', $limit = null)
    {
        $list = model('hotel_collect')->getList($condition, $field, $order, '', '', '', $limit);
        return $this->success($list);
    }

    /**
     * 获取收藏分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getCollectPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'hc.create_time desc', $field = 'hc.collect_id, hc.member_id, hc.hotel_id, h.hotel_name,h.hotel_img,h.direct,h.full_address,h.address,h.longitude,h.latitude,h.label_format')
    {
        $alias = 'hc';
        $join  = [
            ['hotel h', 'hc.hotel_id = h.hotel_id', 'inner'],
        ];
        $list  = model('hotel_collect')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
        return $this->success($list);
    }
}